import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);

export const createStore = () => {
  return new Vuex.Store({
    state: () => ({
      posts: []
    }),
    mutations: {
      setPosts(state, payload) {
        state.posts = payload;
      }
    },
    actions: {
      // 重要！！！！！在服务端渲染期间 务必让 action 返回一个 promise
      async getPosts(context) {
        const { data } = await axios.get('https://conduit.productionready.io/api/articles');
        context.commit('setPosts', data.articles);
      }
    }
  });
}